<?php 

namespace App\Http\Controllers\Admin;

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Admin\ThrottlesLogins;
use Illuminate\Foundation\Admin\AuthenticatesAndRegistersUsers;


class AttributeController extends Controller
{
	public function att_add($type_id){
		if($_POST){
			$attr= new \App\attr();
			$post=array();
        	if(isset($_POST['attr_value'])) {
                $post['attr_value'] = str_replace("\r\n", "|", $_POST['attr_value']);
            }
            $post['attr_name']=$_POST['attr_name'];
            $post['type_id']=$_POST['type_id'];
            $post['attr_type']=$_POST['attr_type'];
            $post['attr_input_type']=$_POST['attr_input_type'];
            $types=$_POST['types'];
            $res=$attr->insert_do($post);
            if($res){
                return redirect('/admin/att_list/'.$types);
            }else{
                echo "<script>alert('分类添加失败');location.href='att_add';</script>";
            }
		}else{
			$type= new \App\Type();
			$res=$type->NgetAll();
			// print_r($res);
			return view('Admin.Attribute.attribute_add',['res'=>$res,'type'=>$type_id]);
		}
	}

	public function att_list($type_id){
		$attr= new \App\attr();
		$res=$attr->getSome($type_id);
		return view('Admin.Attribute.attribute_list',['res'=>$res,'type_id'=>$type_id]);
	}

	public function att_edit($type_id,$attr_id){
		$attr= new \App\attr();
		if($_POST){
   			$attr_id=$_POST['attr_id'];
   			$types=$_POST['types'];
   			$arr=array(
   				'attr_name'=>$_POST['attr_name'],
   				'type_id'=>$_POST['type_id'],
   				'attr_type'=>$_POST['attr_type'],
   				'attr_input_type'=>$_POST['attr_input_type'],
   				'attr_value'=>$_POST['attr_value']
   			);
   			$res=$attr->updateOne($attr_id,$arr);
   			if($res){
                return redirect('/admin/att_list/'.$types);
            }else{
                echo "<script>alert('分类修改失败');location.href='att_list';</script>";
            }
		}else{
			$type= new \App\Type();
			$res=$type->NgetAll();
			$result=$attr->getOne($attr_id);
			foreach ($result as $key => &$value) {
				$value['attr_value']=str_replace("|", "\r\n", $value['attr_value']);
			}
			return view('Admin.Attribute.attribute_edit',['res'=>$res,'type'=>$type_id,'result'=>$result[0]]);
		}
		
	}

	public function att_del($type_id,$attr_id){
		$attr= new \App\attr();
		$res=$attr->getField($attr_id);
		if($res){
                return redirect('/admin/att_list/'.$type_id);
            }else{
                echo "<script>alert('分类删除失败');location.href='att_list';</script>";
            }
	}
}